package cn.com.callback

import cn.com.elite.EliteBean;
import cn.com.kh.KHDBConfig
import cn.com.kh.KHUtil;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory
import cn.com.elite.Util
import org.springframework.jdbc.core.JdbcTemplate;

/**
 * @Title:
 * @Description:
 * @Param: 电销自定义配置分配
 * @return:
 * @Author: CC
 * @Date: 2023/9/9
 */

public class autoKFReallocation implements CallBackInvoke {
    private static final Log log = LogFactory.getLog(autoKFReallocation.class);

    public void doCallBack(EliteBean eliteBean, String callBack, Map map, String body) {

        try {
            long startTime = System.currentTimeMillis();
            JdbcTemplate jt = new JdbcTemplate(eliteBean.getDataSource());
            //查找实时数据配置
            String configSql = ''' 
                             select r.rec_guid,
                             r.pz_name,
                             r.staff_id,
                             r.rolegroup_id,
                             r.cloud_state,
                             r.createddate,
                             r.f_Operation_Step
                             from pa_ss_config r 
                             where r.state='0'
                             order by r.sort asc 
                             limit 1 
                             ''';

            List<Map<String, Object>> configList = jt.queryForList(configSql);

            if (configList.size() <= 0) {
                log.debug("【电销数据实时分配】：未查询到配置信息,不往下执行！");
                return;

            }


            String config_staff_id = configList.get(0).get("staff_id");//人员
            String config_rolegroup_id = configList.get(0).get("rolegroup_id");//组别
            String config_f_Operation_Step = configList.get(0).get("f_Operation_Step");//操作步骤
            String config_cloud_state = configList.get(0).get("cloud_state");//云平台状态
            String config_name = configList.get(0).get("pz_name");//配置名称


            //是否需要拨打状态为是的客户---默认最近7天的数据-------c.appointment_time >=e.MODIFIEDDATE
            String selectSql = '''
                            Select 
                                  distinct
                                  e.HANDLEBY_ID,
                                  c.f_cloud_state,
                                  c.f_operation_step,
                                  e.elitetask_guid, 
                                  e.Objective_GUID,
                                  e.customer_guid
                                  from elitetask e 
                                  left join customer c on e.customer_guid=c.customer_guid
                                  where 1=1
                                  and c.appointment_time>=DATE_SUB(CURDATE(), INTERVAL 7 DAY)
                                  and  e.Handlegroup_Id='LMGVGI'
                                  and e.MAP_ID!='M4F19B'
                              ''';

            List<Map<String, Object>> selelctList = jt.queryForList(selectSql);

            log.debug("[配置组别]：" + config_rolegroup_id + "[配置人员]:" + config_staff_id + "[配置操作步骤]:" + config_f_Operation_Step + "[配置云平台状态]：" + config_cloud_state + "[配置名称：]" + config_name);

            for (int i = 0; i < selelctList.size(); i++) {
                String HANDLEBY_ID = selelctList.get(i).get("HANDLEBY_ID");
                String f_cloud_state = selelctList.get(i).get("f_cloud_state");
                String f_operation_step = selelctList.get(i).get("f_operation_step");
                String elitetask_guid = selelctList.get(i).get("elitetask_guid");
                String Objective_GUID = selelctList.get(i).get("Objective_GUID");
                String customer_guid = selelctList.get(i).get("customer_guid");
                boolean isflag = true;
                log.debug("[elitetask_guid]：" + elitetask_guid + "[Objective_GUID]:" + Objective_GUID + "[操作步骤]:" + f_operation_step + "[平台状态]：" + f_cloud_state);

                if (KHUtil.isNull(config_f_Operation_Step) && !KHUtil.isNull(f_cloud_state)) {

                    //操作步骤等于空,云平台状态不等于空str1.indexOf(str2
                    isflag = config_cloud_state.contains(f_cloud_state);

                } else if (KHUtil.isNull(config_cloud_state) && !KHUtil.isNull(f_operation_step)) {

                    isflag = config_f_Operation_Step.contains(f_operation_step);

                } else {
                    isflag = false;

                }

                if (HANDLEBY_ID.equals(config_staff_id)) {
                    isflag = false;
                    log.debug("当前人员与配置人员重复,不执行！");

                }


                if (isflag) {
                    String[] custTaskUpdateSQL = new String[3];
                    //1.更新 elitetask表Hanleby_ID,Handlegroup_Id
                    custTaskUpdateSQL[0] = "Update elitetask  Set Handleby_Id='" + config_staff_id + "',Handlegroup_Id='" + config_rolegroup_id + "'   Where Objective_GUID='" + Objective_GUID + "' ";
                    //2.更新 objective表Hanleby_ID,Handlegroup_Id
                    custTaskUpdateSQL[1] = "Update objective Set Handleby_Id='" + config_staff_id + "',Handlegroup_Id='" + config_rolegroup_id + "'     Where  Objective_GUID='" + Objective_GUID + "'";
                    //写入顾问分配历史表
                    custTaskUpdateSQL[2] = "insert into hst_Service_hislist (sys_guid,customer_guid,used_staffid,apply_time,MODIFIEDBY,context,Reviewer_staffid,Reviewer_time,OBJECTIVE_GUID)values(UUID(),'" + customer_guid + "','" + config_staff_id + "',now(),'" + 'SELITE' + "','【实时数据配置】电销人员更换','SELITE',now(),'" + Objective_GUID + "')";
                    int[] batchSQLUpdateResult = eliteBean.updateBatch(custTaskUpdateSQL, "dataSource");
                    log.debug("[更换人员更新结果集返回]:" + batchSQLUpdateResult);
                } else {
                    log.debug("不符合配置规则,不执行！")
                }
            }


            long endtime = System.currentTimeMillis();
            log.debug("【电销自定义配置分配】执行耗时:" + (endtime - startTime));

        } catch (Exception e) {
            log.error("电销自定义配置分配异常!", e);
        }

    }
}